#include <QtTest>
#include <QSignalSpy>

#include "test_PlaylistAutomation.h"
#include "MockMediaPlugin.h"
#include "MockMediaSourceFactory.h"
#include "MediaListModel.h"
#include "ActionListController.h"
#include "PlaylistAutomation.h"
#include "ExponentialFader.h"

test_PlaylistAutomation::test_PlaylistAutomation()
{
}

/** executed before every test case */
void test_PlaylistAutomation::init()
{
   m_mediaPlugin = new MockMediaPlugin( this);
   MockMediaSourceFactory *sourceFactory = new MockMediaSourceFactory();
   m_mediaModel = new MediaListModel( sourceFactory, this);
   m_actionController = new ActionListController( *m_mediaModel, this);
   m_fader = new ExponentialFader( this);
   m_playlistAutomation = new PlaylistAutomation( m_mediaPlugin, m_mediaModel, m_actionController,
                                                  m_fader, this);

   connect( m_actionController, SIGNAL(activeRowChanged(int)),
            m_playlistAutomation, SLOT(activateByRowNumer(int)) );
}

/** executed after every test case */
void test_PlaylistAutomation::cleanup()
{
   delete m_playlistAutomation;
   delete m_fader;
   delete m_actionController;
   delete m_mediaModel;
   delete m_mediaPlugin;
}

void test_PlaylistAutomation::initTestCase()
{
}

void test_PlaylistAutomation::cleanupTestCase()
{
}

void test_PlaylistAutomation::testInstance()
{
   QVERIFY( m_playlistAutomation != NULL);
}

void test_PlaylistAutomation::testActiveChangeRequest()
{
   m_mediaModel->addMediaFile("C:/Fake/Path/track.mp3");
   m_playlistAutomation->activateByRowNumer( 0);

   MockMediaPlugin *mediaPlugin = dynamic_cast<MockMediaPlugin *>(m_mediaPlugin);
   QCOMPARE( mediaPlugin->spyLastSource()->fileName(), QString("C:/Fake/Path/track.mp3") );
}

void test_PlaylistAutomation::testActiveChangedById()
{
   m_mediaModel->addMediaFile("C:/Fake/Path/track.mp3");
   m_mediaModel->addMediaFile("C:/Fake/Path/track.mp3");

   /* activate second track without playing */
   m_playlistAutomation->activateById( QString("track.mp3 (2)"), false);

   MockMediaPlugin *mediaPlugin = dynamic_cast<MockMediaPlugin *>(m_mediaPlugin);
   QVERIFY( mediaPlugin->spyLastSource() != NULL);
   QCOMPARE( mediaPlugin->spyLastSource()->fileName(), QString("C:/Fake/Path/track.mp3") );
   QCOMPARE(m_actionController->activeItemRow(), 1);
   QCOMPARE( (int)m_mediaPlugin->state(), (int)(AudioObject::PausedState));
}

void test_PlaylistAutomation::testTryToActivateInvalidId()
{
   bool exceptionThrown = false;

   try
   {
      m_playlistAutomation->activateById( QString("DoesNotExist.mp3"), false);
   }
   catch( QString & error)
   {
      exceptionThrown = true;
      QCOMPARE( error, QString("No media has name: DoesNotExist.mp3"));
   }

   QVERIFY( exceptionThrown == true);
}

void test_PlaylistAutomation::testStopOnActiveChanged()
{
   m_mediaModel->addMediaFile("C:/Fake/Path/track1.mp3");
   m_mediaModel->addMediaFile("C:/Fake/Path/track2.mp3");
   m_playlistAutomation->activateByRowNumer( 0);
   m_mediaPlugin->play();
   QCOMPARE( (int)m_mediaPlugin->state(), (int)(AudioObject::PlayingState));

   m_playlistAutomation->activateByRowNumer( 1);
   QCOMPARE( (int)m_mediaPlugin->state(), (int)(AudioObject::PausedState));
}

void test_PlaylistAutomation::testStopOnClose()
{
   m_mediaModel->addMediaFile("C:/Fake/Path/track.mp3");
   m_playlistAutomation->activateByRowNumer( 0);

   m_mediaPlugin->play();
   m_playlistAutomation->onClosing();
   QCOMPARE( (int)m_mediaPlugin->state(), (int)(AudioObject::StoppedState));
}

void test_PlaylistAutomation::testActivateNextWhenPreviousIsOver()
{
   m_mediaModel->addMediaFile("C:/Fake/Path/track1.mp3");
   m_mediaModel->addMediaFile("C:/Fake/Path/track2.mp3");

   /* This calls m_playlistAutomation::activateByRowNumer
    */
   m_actionController->activateRequest( m_mediaModel->index(0));

   m_mediaPlugin->play();
   m_mediaPlugin->finished();

   MockMediaPlugin *mediaPlugin = dynamic_cast<MockMediaPlugin *>(m_mediaPlugin);
   QVERIFY( mediaPlugin->spyLastSource() != NULL);
   QCOMPARE( mediaPlugin->spyLastSource()->fileName(), QString("C:/Fake/Path/track2.mp3") );
   QCOMPARE( (int)m_mediaPlugin->state(), (int)(AudioObject::PausedState));
}
